<?php

namespace App\Http\Controllers\Api\Train\Trip;

use App\Http\Controllers\Controller;
use App\Models\Task\SnapTaskAssist\AcceleratorPack;
use App\Models\Task\SnapTaskAssist\SnapAcceleratorPacks;
use App\Models\Task\SnapTasks\Repositories\Interfaces\SnapTaskRepositoryInterface;
use App\Models\Task\SnapTasks\SnapTask;
use Exception;
use Illuminate\Http\Request;

class SnapInviteController extends Controller
{

    private string|null                 $user_id;
    private SnapTaskRepositoryInterface $snapTaskRepository;

    public function __construct(SnapTaskRepositoryInterface $snapTaskRepository)
    {
        $this->snapTaskRepository = $snapTaskRepository;
        $this->user_id            = auth()->id() ?: '';
    }

    public function invite(Request $request)
    {
        $task_id   = $request->input('task_id');
        $task_info = $this->snapTaskRepository->findOneByTaskId($task_id);
        if (SnapTask::TASK_STATE_WAITING_PAY == $task_info->getTaskState()) {
            return $this->error(1, "助力邀请不存在,订单未支付");
        }

        $sap = new SnapAcceleratorPacks();
        return $this->success('', $this->user_id ? $sap->InviteInfo($task_info, $this->user_id) : []);
    }

    /**
     * @throws Exception
     */
    public function assist(Request $request)
    {
        $task_id   = $request->input('task_id');
        $task_info = $this->snapTaskRepository->findOneByTaskId($task_id);
        $this->snapTaskRepository->userAssist($task_info, new AcceleratorPack($this->user_id));
        return $this->success("已帮好友助力成功！谢谢");
    }
}
